<?php
/**
 * Created by PhpStorm.
 * User: zlc
 * Date: 2018-07-19
 * Time: 上午 11:43
 * 订单管理
 */
namespace app\admin\controller;
use app\admin\model\IllegalModel;
use app\api\controller\Excel;
class Order extends Base{
    public function ruleOrder(){
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['phone'])) {
                $where['snake_member.member_mobile'] = ['like', '%' . $param['phone'] . '%'];
            }
            if (!empty($param['type'])&&$param['type']!=200) {
                $where['snake_illegal.illegal_status'] = $param['type'];
            }
            if (!empty($param['name'])) {
                $where['snake_member.member_name'] = ['like', '%' . $param['name'] . '%'];
            }
            if (!empty($param['order_sn'])) {
                $where['snake_illegal.illegal_order_sn'] = ['like', '%' . $param['order_sn'] . '%'];
            }
            if (!empty($param['car_number'])) {
                $where['snake_illegal.carNo'] = ['like', '%' . $param['car_number'] . '%'];
            }
            $join = "snake_member";
            $condition="snake_illegal.member_id = snake_member.member_id";
            $illegal =  new IllegalModel();
            $selectResult = $illegal->getIllegalByWhere($where, $offset, $limit,$join,$condition);
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['illegal_status'] = $illegal->getIllegalStatus($vo['illegal_status']);
                $selectResult[$key]['illegal_downtime'] = date('Y-m-d H:i:s',$vo['illegal_downtime']);
                $selectResult[$key]['checked'] = "<input type='checkbox' name='check' value='".$vo['illegal_id']."'>";
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['illegal_id']));
            }

            $return['total'] = $illegal->getIllegalCount($where,$join,$condition);  // 总数据
            $return['rows'] = $selectResult;

            return json($return);
        }
        return $this->fetch();
    }
    //违章删除
    public function illegalDel(){
        $illegal = new IllegalModel();
        $param = input('param.');
        if(!$param['id']){
            ajaxReturn(-1,'删除失败');
        }
        $map = " illegal_id = ".$param['id'];
        $res = $illegal->getIllegalDel($map);
        if($res === false){
            ajaxReturn(-1,'操作失败');
        }else{
            ajaxReturn(1,'操作成功');
        }
    }
    //违章订单导出
    public function illegalOut(){
        $param = input('param.');
        $where = [];
        if (!empty($param['phone'])) {
            $where['snake_member.member_mobile'] = ['like', '%' . $param['phone'] . '%'];
        }
        if (!empty($param['type'])&&$param['type']!=200) {
            $where['snake_illegal.illegal_status'] = $param['type'];
        }
        if (!empty($param['name'])) {
            $where['snake_member.member_name'] = ['like', '%' . $param['name'] . '%'];
        }
        if (!empty($param['order_sn'])) {
            $where['snake_illegal.illegal_order_sn'] = ['like', '%' . $param['order_sn'] . '%'];
        }
        if (!empty($param['car_number'])) {
            $where['snake_illegal.carNo'] = ['like', '%' . $param['car_number'] . '%'];
        }
        $join = "snake_member";
        $condition="snake_illegal.member_id = snake_member.member_id";
        $illegal =  new IllegalModel();
        $selectResult = $illegal->getIllegalOutByWhere($where,$join,$condition);
        foreach($selectResult as $key=>$vo){
            $selectResult[$key]['illegal_status'] = $illegal->getIllegalStatus($vo['illegal_status']);
            $selectResult[$key]['illegal_downtime'] = date('Y-m-d H:i:s',$vo['illegal_downtime']);
        }
        $Excel = new Excel();
        $filename = '违章订单';
        $title = array('会员名称','会员手机号','订单号','违章人','违章人电话','违章车辆','通知书号','违章时间','违法行为','违法城市','违法地址','滞纳金','服务费','罚款金额','滞纳天数','总金额','订单状态','下单时间');
        $field = array('member_name','member_mobile','illegal_order_sn','contactName','tel','carNo','illegal_no','illegal_time','illegal_action','illegal_place','address','overdueFine','serviceFee','fakuan','overdueDays','illegal_price','illegal_status','illegal_downtime');
        $Excel->out($filename,$filename,17,$title,$field,$selectResult);die;
    }
    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '删除' => [
                'auth' => 'order/illegaldel',
                'href' => "javascript:illegalDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }
}